package com.spider.ask120.webmagic;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdiy.core.Rs;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.spider.ask120.service.ChannelArticleInfoService;
import com.spider.utils.DateUtil;
import com.spider.utils.ElementsComparator;
import com.spider.utils.Function;

public class Base {
	protected static Function fun = new Function();
	protected static String date = DateUtil.monthDateT();
	public static final String UTF_8 = "UTF-8";
	public static final String GBK = "GBK";
	
	protected static Integer PINDAO_CHANNEL_PID = 1145;
	protected static Integer JINGYAN_CHANNEL_PID = 1144;
	protected static String BAIKE_CHANNEL_PID = "1";
	protected static String NEWS_CHANNEL_PID = "9";
	protected static String NEWS_SCHEDULER_PATH = "news_scheduler_path";
	protected static String NEWS_PIC_FILE_PATH = "news_pic_file_path";
	protected static String BAIKE_PIC_FILE_PATH = "baike_pic_file_path";
	protected static String BAIKE_SCHEDULER_PATH = "baike_scheduler_path";
	protected static String JIBING_PIC_FILE_PATH="jibing_pic_file_path";
	protected static String JIBING_SCHEDULER_PATH = "jibing_scheduler_path";
	protected static String JINGYAN_PIC_FILE_PATH = "jingyan_pic_file_path";
	protected static String JINGYAN_SCHEDULER_PATH = "jingyan_scheduler_path";
	protected static String PINDAO_SCHEDULER_PATH = "pindao_scheduler_path";
	protected static String PINDAO_PIC_FILE_PATH = "pindao_pic_file_path";
	protected static String PINDAO_PAGE_INDEX = "pindao_page_index";
	protected static String JINGYAN_PAGE_INDEX = "jingyan_page_index";
	protected static String BAIKE_PAGE_INDEX = "baike_page_index";
	protected static String ARTICLE_STR_ID = "article_str_id";
	
	/**
	 * 替换内容中图片地址为本地地址
	 * @param content html内容
	 * @param pic_dir 本地地址文件路径
	 * @return html内容
	 */
	public String replace(String content,String pic_dir){
		String str = content;
		String cont = content;
		while (true) {
			int i = str.indexOf("src=\"");
			if (i != -1) {
				str = str.substring(i+5, str.length());
				int j = str.indexOf("\"");
				String pic_url = str.substring(0, j);
				//下载图片到本地并返回图片地址
				String pic_path = fun.downloadPic(pic_url,pic_dir);
				cont = cont.replace(pic_url, pic_path);
				str = str.substring(j,str.length());
				//System.out.println(str);
			} else{
				break;
			}
		}
		return cont;
	}
	
	/**
	 * 替换内容中图片地址为本地地址
	 * @param content html内容
	 * @param pic_dir 本地地址文件路径
	 * @return html内容
	 */
	public String replaceForNews(String content,String pic_dir){
		String str = content;
		String cont = content;
		while (true) {
			int i = str.indexOf("src=\"");
			if (i != -1) {
				str = str.substring(i+5, str.length());
				int j = str.indexOf("\"");
				String pic_url = str.substring(0, j);
				//下载图片到本地并返回图片地址
				String pic_path = fun.downloadPicForNews(pic_url,pic_dir);
				cont = cont.replace(pic_url, pic_path);
				str = str.substring(j,str.length());
				//System.out.println(str);
			} else{
				break;
			}
		}
		return cont;
	}
	
	/**
	 * unicode转utf8
	 * @param theString
	 * @return
	 */
	public static String unicodeToUtf8(String theString) {
		if (null == theString || theString.isEmpty()) {
			return null;
		}
		char aChar;
		int len = theString.length();
		StringBuffer outBuffer = new StringBuffer(len);
		for (int x = 0; x < len;) {
			aChar = theString.charAt(x++);
			if (aChar == '\\') {
				aChar = theString.charAt(x++);
				if (aChar == 'u') {
					// Read the xxxx
					int value = 0;
					for (int i = 0; i < 4; i++) {
						aChar = theString.charAt(x++);
						switch (aChar) {
						case '0':
						case '1':
						case '2':
						case '3':
						case '4':
						case '5':
						case '6':
						case '7':
						case '8':
						case '9':
							value = (value << 4) + aChar - '0';
							break;
						case 'a':
						case 'b':
						case 'c':
						case 'd':
						case 'e':
						case 'f':
							value = (value << 4) + 10 + aChar - 'a';
							break;
						case 'A':
						case 'B':
						case 'C':
						case 'D':
						case 'E':
						case 'F':
							value = (value << 4) + 10 + aChar - 'A';
							break;
						default:
							throw new IllegalArgumentException(
									"Malformed   \\uxxxx   encoding.");
						}
					}
					outBuffer.append((char) value);
				} else {
					if (aChar == 't')
						aChar = '\t';
					else if (aChar == 'r')
						aChar = '\r';
					else if (aChar == 'n')
						aChar = '\n';
					else if (aChar == 'f')
						aChar = '\f';
					outBuffer.append(aChar);
				}
			} else
				outBuffer.append(aChar);
		}
		return outBuffer.toString();
	}
	/**
	 * properties中保存pageIndex
	 * @param key
	 * @param index
	 */
	public void savePageIndex(String key, Integer index){
		Map<String,String> map = new HashMap<String, String>();
		map.put(key, index+"");
		fun.setProValue(map);
	}
	/**
	 * 向properties文件保存数据
	 * @param key
	 * @param value
	 */
	public void saveId(String key, String value){
		Map<String,String> map = new HashMap<String, String>();
		map.put(key, value);
		fun.setProValue(map);
	}
	/**
	 * 获取频道对应str_id
	 * @param channel_id
	 * @return str_id的int值
	 */
	public Integer getArtStrId(Integer channel_id){
		Rs info = ChannelArticleInfoService.getInfoByChId(channel_id);
		return info.getInt("article_str_id");
	}
	/**
	 * 对elements重新排序
	 * @param elements
	 * @return
	 */
	public List<Element> sort(Elements elements){
		List<Element> eles = new ArrayList<Element>();
		for (Element element : elements) {
			eles.add(element);
		}
		Comparator<Element> comparator = new ElementsComparator<Element>();
		Collections.sort(eles,comparator);
		return eles;
	}
}
